package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public Page<Setmeal>findByPage (String name,Integer pageNum, Integer pageSize){
        //根据name查询套餐
        Page<Setmeal> page = new Page<>(pageNum,pageSize);

        //查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);

        page= setmealMapper.selectPage(page,wrapper);

        //遍历，判断
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)){
            for (Setmeal setmeal : setmealList){
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
            }
        }
        return page;
    }
    @Override
    public void save(Setmeal setmeal) {
        //1. 将基本信息保存到setmeal表
        setmealMapper.insert(setmeal);

        //2. 遍历前端传入的菜品信息
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //3. 设置上套餐id
                setmealDish.setSetmealId(setmeal.getId());

                //4. 将套餐菜品保存到setmeal_dish表
                setmealDishMapper.insert(setmealDish);
            }
        }

    }
    //删除：先判断状态，停售可以删除
    @Override
    public void deleteByIds(Long [] ids){
        //状态为1是在售。状态0是停售
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus,1).in(Setmeal::getId,ids);
        Integer num = setmealMapper.selectCount(wrapper);
        if (num >0){
            throw  new CustomException("正在出售，无法删除");
        }
        //删除中间表
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);
        //删除套餐
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getStatus,0).in(Setmeal::getId,ids);
        setmealMapper.delete(wrapper2);
    }
    @Override
    public List<Setmeal> findByCategoryId(Long categoryId , Integer status){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();//sql
        wrapper.eq(Setmeal::getCategoryId,categoryId)//和id对比
                .eq(Setmeal::getStatus,status);//判断状态

        return setmealMapper.selectList(wrapper);

    }

    //回显
    @Override
    public  Setmeal  findById(Long id){
        //查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //根据catrory_id查套餐的名字
        Category category = categoryMapper.selectById(id);
        setmeal.setCategoryName(category.getName());

        //根据setmeal的id查
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        setmeal.setSetmealDishes(setmealDishList);

        return setmeal;
    }
    @Override
    public void updateByIds(Long [] ids,Integer status){
        for (Long id:ids){
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }

    }
    @Override
    public void update(Setmeal setmeal) {
        //1. 根据主键更新菜品表信息
        setmealMapper.updateById(setmeal);

        //2. 根据套餐id删除中间表中相关菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //3. 向中间表保存新的菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

}
